<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sign your Y account</title>
    <link rel="stylesheet" href="css/loginstyle.css">
    <link rel="icon" href="img/favicon.png" type="image/x-icon">
    <script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>
<video id="v1" autoplay loop muted>
    <source src="img/Background.mp4" type="video/mp4"  />
</video>
<div class="container">
    <h1>Sign your Y account</h1>
    <form id="registerForm">
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" id="name" name="name" placeholder="" required>
        </div>
        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" id="username" name="username" placeholder="" required>
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" id="email" name="email" placeholder="" required>
        </div>
        <div class="form-group">
            <label for="password" >Password</label>
            <input type="password" id="password" name="password" placeholder="6+ characters" required>
        </div>
        <div class="form-group">
            <input type="checkbox" id="terms" name="terms" required>
            <label for="terms">
                I agree with Y's <a href="#">Terms of Service</a>, <a href="#">Privacy Policy</a>, and default <a href="#">Notification Settings</a>.
            </label>
        </div>
        <button type="submit">Create Account</button>
        <p>Already have an account? <a href="login.html">Sign In</a></p>
    </form>
</div>

<script>
    $(document).ready(function() {
        $('#registerForm').submit(function(event) {
            event.preventDefault();

            var password = $('#password').val();
            if (password.length < 6) {
                alert('密码应大于六个字符');
                return;
            }

            var formData = {
                name: $('#name').val(),
                username: $('#username').val(),
                email: $('#email').val(),
                password: password
            };

            $.ajax({
                type: 'POST',
                url: '../../java/com/qdu/servlet/RegisterServlet.java',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(response) {
                    alert('注册成功!');
                    window.location.href = 'index.html';
                },
                error: function(xhr, status, error) {
                    alert('Registration failed: ' + xhr.responseText);
                }
            });
        });
    });
</script>
</body>
</html>
